<?php

function reequipment_userapi_getmodels($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['remodels'];
    $join = $xartable['reareas'];

    $query = "SELECT DISTINCT m.model_id, m.area_id, a.area_name, m.model_name, m.current"
    		. " FROM $table m"
    		. " LEFT OUTER JOIN $join a"
    		. " on m.area_id = a.area_id";
    $where = array();
    $bind = array();

    if (!empty($model_id)) {
        $where[] = 'm.model_id = ?';
        $bind[] = (int)$model_id;
    }
    
    if (!empty($area_id)) {
        $where[] = 'a.area_id = ?';
        $bind[] = (int)$area_id;
    }

    if (!empty($model_name)) {
        $where[] = 'm.model_name = ?';
        $bind[] = (string)$model_name;
    }
    
    if (!empty($current)) {
        $where[] = 'm.current = ?';
        $bind[] = (int)$current;
    }    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY m.area_id";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($model_id, $area_id, $area_name, $model_name, $current) = $result->fields;

        $tags[] = array(
                'model_id'        => $model_id,
                'area_id'        => $area_id,
                'area_name'		  => xarVarPrepForDisplay($area_name),
                'model_name'      => xarVarPrepForDisplay($model_name),
                'current'		  => $current
                
        );
    }

    $result->Close();
    return $tags;
}
?>